package top.csy8888.ch08.web.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import top.csy8888.ch08.common.ResultVO;
import top.csy8888.ch08.entity.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author CSY
 * @date 2025 - 09 - 17
 */
@RestController
@RequestMapping("/api/v1")
//使用@Tag注解对当前的API进行简要描述
@Tag(name = "用户服务接口",description = "提供用户信息相关的API操作接口")
public class UserController {

    private final static List<User> users;
    static {
        User u1 = new User(1,"张三",18);
        User u2 = new User(2,"李四",20);
        User u3 = new User(3,"王五",21);
        users = Arrays.asList(u1,u2,u3);
    }
    /**
     * 查询所有用户
     * （注意：请求url中不应该出现动词，由请求方法决定操作行为）
     * @return
     */
    @GetMapping("/users")
    @Operation(description = "查询系统中所有用户的基本信息",method = "GET", summary = "查询用户列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "响应成功"),
            @ApiResponse(responseCode = "500",description = "查询失败，服务器错误")
    })
    public ResultVO<List<User>> listUsers(){
        return ResultVO.success(users);
    }

    @GetMapping("/user/{id}")
    //方法描述
    @Operation(description = "根据id查询用户的基本信息",method = "GET",summary = "查询用户")
    //响应描述
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "响应成功"),
            @ApiResponse(responseCode = "500",description = "查询失败，服务器错误")
    })
    //参数描述
    //required属性表示必须传参
    //in表示参数通过什么途径传入（请求头，请求参数，路径参数）（ParameterIn.PATH表示参数通过路径传递）
    @Parameters({
            @Parameter(description = "用户id",name = "id",required = true,in = ParameterIn.PATH)
    })
    public ResultVO<User> getUser(@PathVariable("id") Integer id){
        for (User user : users) {
            if (id == user.getId()) {
                return ResultVO.success(user);
            }

        }
        return ResultVO.error(404,"没有该用户信息！");
    }

    @PostMapping("/user")
    @Operation(description = "根据传入的User对象添加用户",summary = "添加用户",method = "POST")
    @Parameters({
            //in = ParameterIn.DEFAULT为默认可以不用设置
            @Parameter(name = "user",required = true,in = ParameterIn.DEFAULT)
    })
    public ResultVO<String> addUser(@RequestBody User user){
        return ResultVO.success();
    }
}
